package com.ailot.cloud.gateway.filter;

import com.ailot.cloud.base.common.constants.SecurityConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


/**
 * 全局拦截器，作用所有的微服务
 */
@Slf4j
@Component
public class BaseRequestGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        // 清洗请求头中的内部调用标记，防止非法请求
        ServerHttpRequest request = exchange.getRequest().mutate()
                .headers(httpHeaders -> httpHeaders.remove(SecurityConstant.FROM))
                .build();
        log.info("request path:{}", request.getPath());
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -100;
    }
}
